﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ClipboardManager
{
    public partial class FormClipboardItem : Form
    {
        private ClipboardItem clipItem;

        public FormClipboardItem(ClipboardItem cbItem)
        {
            InitializeComponent();

            clipItem = cbItem;

            if (clipItem.ClipboardType == ClipboardItem.Type.Text ||
                clipItem.ClipboardType == ClipboardItem.Type.Html ||
                clipItem.ClipboardType == ClipboardItem.Type.Files)
                ShowText(clipItem.DataString, false);

            else if (clipItem.ClipboardType == ClipboardItem.Type.Rtf)
                ShowText(clipItem.DataString, true);

            else if (clipItem.ClipboardType == ClipboardItem.Type.Bitmap)
                ShowImage(clipItem.DataBitmap);


            lblFormat.Text = cbItem.info.Format;
            lblFormats.Text = Helper.ArrToString(cbItem.info.Formats);

        }

        private void ShowText(string text, bool IsRtf)
        {
            if (IsRtf) richTextBox1.Rtf = text;
            else       richTextBox1.Text = text;

            richTextBox1.SelectAll();

            int LongestLine = 1;
            foreach (string i in richTextBox1.Lines)
            {
                if (i.Length > LongestLine) LongestLine = i.Length;
            }

            this.Width = (int)richTextBox1.SelectionFont.SizeInPoints * LongestLine;
            this.Height = (richTextBox1.Lines.Length * (int)(richTextBox1.SelectionFont.Height)) + 64;

            richTextBox1.Select(0, 0);
            richTextBox1.BringToFront();

            lblLength.Text = clipItem.info.Length.ToString();
            lblName.Text = clipItem.info.WindowText;
            lblTime.Text = clipItem.info.Time.ToString("HH:mm:ss MM/dd");
        }

        private void ShowImage(Bitmap Image)
        {
            pictureBox1.Image = Image;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

            this.Height = 400;
            this.Width = 350;

            pictureBox1.Visible = true;
            button1.Enabled = false;
            pictureBox1.BringToFront();

            lblLength.Text = clipItem.info.Length;
            lblName.Text = clipItem.info.WindowText;
            lblTime.Text = clipItem.info.Time.ToString("H:m:s MM/dd");
        }

        private void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            clipItem.SetItemToClipboard();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            string TextToFormat = richTextBox1.Text;
            string FormatedText = Helper.Trim(TextToFormat);
            richTextBox1.Clear();
            richTextBox1.Text = FormatedText;
        }
    }
}
